<script lang="ts">
export default {
  name: 'examine',
  description: '审核配置',
};
</script>

<script setup lang="ts">
import { ElForm, ElMessage,ElMessageBox } from 'element-plus';
import { nextTick, onMounted, reactive, ref, toRefs } from 'vue';
import type { Action } from 'element-plus'
import { Search, Refresh, Plus } from '@element-plus/icons-vue';
import { ExamineQuery, Examine, ExamineForm } from '@/api/system/examine/types';
import {
  businessOptions,
  checkOptions,
  casualCheckPages,
  casualCheckAdd,
  casualCheckEdit,
  internalOptions,
  enterpriseOptions,
  personOptions,
  engineerOptions,
  casualCheckDel
} from '@/api/system/examine/index';
import {
  subaccountOptions
} from '@/api/occupant/subuser';

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);
const state = reactive({
  loading: false,
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  } as ExamineQuery,
  dataList: [] as Examine[],
  total: 0,
  dialog: {
    title: '',
    visible: false,
  } as DialogType,
  formData: {} as ExamineForm,
  disabled:false,
  rules: {
    businessAuditorType: [
      { required: true, message: '请选择审核人类型', trigger: 'change' },
    ],
    businessType: [
      { required: true, message: '请选择业务类型', trigger: 'change' },
    ],
    checkId: [{ required: true, message: '请选择审核人', trigger: 'change' }],
    principalAccount: [{ required: true, message: '请选择账号', trigger: 'change' }],
    sort: [{ required: true, message: '请填写排序', trigger: 'change' }],
    remark: [{ required: false, message: '请填写备注', trigger: 'change' }],
  },
  checkOption: [] as OptionType[],
  businessOption: [] as OptionType[],
  internalOption: [] as OptionType[],
  enterpriseOption: [] as OptionType[],
  personOption: [] as OptionType[],
  engineerOption: [] as OptionType[],
  option: [] as OptionType[],
  userOption: [] as OptionType[],
});

const {
  loading,
  queryParams,
  dataList,
  total,
  dialog,
  formData,
  rules,
  checkOption,
  businessOption,
  internalOption,
  enterpriseOption,
  personOption,
  engineerOption,
  option,
  userOption,
  disabled
} = toRefs(state);
/**
 * 下拉框查询
 */
function options() {
  businessOptions().then((res: any) => {
    businessOption.value = res.data;
  });
  internalOptions().then((res: any) => {
    internalOption.value = res.data;
    checkOptions().then((res1: any) => {
      checkOption.value = res1.data;
      auditorChange(res1.data[0].value)
    });
  });
  enterpriseOptions().then((res: any) => {
    enterpriseOption.value = res.data;
  });
  personOptions().then((res: any) => {
    personOption.value = res.data;
  });
  engineerOptions().then((res: any) => {
    engineerOption.value = res.data;
  });
  subaccountOptions().then((res:any)=>{
    console.log(res);
    userOption.value = res.data
  })
}

/**
 * 查询
 */
function handleQuery() {
  loading.value = true;
  casualCheckPages(queryParams.value).then((res: any) => {
    dataList.value = res.data.list;
    total.value = res.data.total;
    loading.value = false;
  });
}
/**
 * 重置查询
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}
/**
 * 新增
 */
function handleAdd() {
  state.formData = {}
  state.formData.sort = 1
  formData.value.businessType =businessOption.value[0].value
  formData.value.businessAuditorType = checkOption.value[0].value;
  state.dialog.visible = true;
  state.dialog.title = '新增';
  state.disabled = false
}
/**
 * 修改
 */
function handleUpdate(row: any) {
  state.dialog.title = '修改';
  toggleOption(state.formData.businessAuditorType);
  nextTick(() => {
    state.formData =Object.assign(row);
    state.disabled = false
    state.formData.checkId = row.checkId - 0;
    state.formData.principalAccount = row.principalAccount + '';
    state.dialog.visible = true;
  });
}
/**
 * 详情
 */
function handleDetails(row: any) {
  state.dialog.title = '详情';
  toggleOption(state.formData.businessAuditorType);
  nextTick(() => {
    state.formData = Object.assign(row);
    state.formData.checkId = row.checkId - 0;
    state.formData.principalAccount = row.principalAccount + '';
    state.dialog.visible = true;
    state.disabled = true
  });
}
/**
 * 删除
 */
function handleDelete(row: any){
  ElMessageBox.confirm(
    '确定删除改审核配置项?',
    '提示',
    {
      distinguishCancelAndClose: true,
      confirmButtonText: '确定',
      cancelButtonText: '取消',
    }
  )
    .then(() => {
      casualCheckDel(row.id).then((res:any)=>{
        if (res.code == '00000') {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          handleQuery()
        }else{
          ElMessage({
            type: 'success',
            message: res.msg,
          })
        }
      })

    })
    .catch((action: Action) => {
      ElMessage({
            type: 'info',
            message: '取消删除',
          })
    })
}
function auditorChange(value: any) {
  console.log(1);

  state.formData.checkId = '';
  toggleOption(value);
}
function toggleOption(value: any) {
  if (value == 1) state.option = state.internalOption;
  if (value == 2) state.option = state.enterpriseOption;
  if (value == 3) state.option = state.personOption;
  if (value == 4) state.option = state.engineerOption;
  console.log(state.option);
}
/**
 * 取消
 */
function closeDialog() {
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  state.dialog.visible = false;
}
/**
 * 确认
 */
function handleSubmit() {
  if (state.dialog.title == '详情') {
    closeDialog();
    return
  }
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      let parmas = Object.assign({}, formData.value);
      parmas.checkName = state.option.filter(
        (el: any) => el.value == state.formData.checkId
      )[0].label;
      console.log(formData.value);
      console.log(parmas);

      if (formData.value.id) {
        casualCheckEdit(parmas).then(() => {
          ElMessage.success('编辑成功');
          closeDialog();
          resetQuery();
        });
      } else {
        casualCheckAdd(parmas).then(() => {
          ElMessage.success('新增成功');
          closeDialog();
          resetQuery();
        });
      }
    }
  });
}
onMounted(() => {
  options();
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="审核人类型" prop="businessAuditorType">
          <el-select style="width: 200px;" v-model="queryParams.businessAuditorType" placeholder="请选择审核人类型">
            <el-option v-for="item in checkOption" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item style="width: 200px;" label="业务类型" prop="businessType">
          <el-select v-model="queryParams.businessType" placeholder="请选择业务类型">
            <el-option v-for="item in businessOption" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="审核人" prop="checkName">
          <el-input v-model="queryParams.checkName" placeholder="请输入审核人" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
          <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card>
      <!--toolbar-->
      <template #header>
        <el-button type="success" :icon="Plus" @click="handleAdd">新增</el-button>
      </template>
      <el-table ref="dataTableRef" v-loading="loading" :data="dataList" highlight-current-row border>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="审核人类型" prop="businessAuditorTypeName" min-width="160" />
        <el-table-column label="业务类型" prop="businessTypeName" min-width="160" />
        <el-table-column label="审核人名称" prop="checkName" min-width="160" />
        <el-table-column label="下单账号" prop="principalAccountName" min-width="160" />
        <el-table-column label="审核顺序" prop="sort" min-width="160" />
        <!-- <el-table-column label="创建人" prop="createBy" min-width="160" />
        <el-table-column label="创建时间" prop="createTime" min-width="160" /> -->

        <el-table-column label="操作" fixed="right" width="200">
          <template #default="scope">
            <el-button type="success" link @click.stop="handleUpdate(scope.row)">
              修改
            </el-button>
            <el-button type="primary" link @click.stop="handleDetails(scope.row)">
              详情
            </el-button>
            <el-button type="danger" link @click.stop="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- pagination -->
      <pagination v-if="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
  </div>

  <!-- dialog -->
  <el-dialog v-if="dialog.visible" :title="dialog.title" v-model="dialog.visible" width="600px" @close="closeDialog">
    <el-form ref="dataFormRef" :model="formData" :disabled='disabled' :rules="rules" label-width="100px">
      <!-- <el-form-item label="审核人类型" prop="businessAuditorType">
        <el-select v-model="formData.businessAuditorType" placeholder="请选择审核人类型" @change="auditorChange">
          <el-option v-for="item in checkOption" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="businessType">
        <el-select v-model="formData.businessType" placeholder="请选择业务类型">
          <el-option v-for="item in businessOption" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="审核人" prop="checkId">
        <el-select v-model="formData.checkId" placeholder="请选择审核人">
          <el-option v-for="item in option" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="下单账号" prop="principalAccount">
        <el-select v-model="formData.principalAccount" placeholder="请选择账号">
          <el-option v-for="item in userOption" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核顺序" prop="sort">
        <el-input-number v-model="formData.sort" style="width: 80px" controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="formData.remark" placeholder="请填写备注"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
